package com.yyh.weknow.Jan2017;

import java.util.Arrays;
import java.util.List;

/**
 * Created by lenovo on 2017/1/19.
 * 问题：请问主程序能否正常运行输出结果？
 * 解析：Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
 * at Jan2017.Ques0119.main(Ques0119.java:10)
 */
public class Ques0119 {

    public static void main(String[] arg){
        List<String> list = Arrays.asList("1", "2");
        for (String str : toArray(list)) {
            System.out.println(str);
        }
    }

    public static <T> T[] toArray(List<T> list){
        T[] t = (T[]) new Object[list.size()];
        for (int i = 0, s = list.size(); i < s; i++) {
            t[i] = list.get(i);
        }
        return t;
    }
}
